package com.ysd.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.ysd.entity.Product;
import com.ysd.entity.ProductCategory;
import com.ysd.util.DBManager;

public class ProductDaoImpl {
	//根据分类Id查询对应产品
	public List<Product> getProductList(int cId){
		
		List<Product> productList =new ArrayList<Product>();
		
		Connection conn = (Connection) DBManager.getConnection();
		Statement st=null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from product where productcategoryId="+cId);
			while (rs.next()) {
				int productId=rs.getInt("productId");
				String productName=rs.getString("productName");
				
				String imgs=rs.getString("imgs");
				Product product=new Product(productId, productName, imgs);
				productList.add(product);
			}
			return productList;
		} catch (SQLException e) {
			
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
			
		}
		
	}
	
	//模糊查询
	public List<Product> getProductList21(String nId){
		List<Product> productList =new ArrayList<Product>();
		Connection conn = (Connection) DBManager.getConnection();
		Statement st=null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			
			rs = st.executeQuery("select * from product where productName like '%"+nId+"%'");
			while (rs.next()) {
				int productId=rs.getInt("productId");
				String productName=rs.getString("productName");
				int productCategoryId=rs.getInt("productCategoryId");
				String imgs=rs.getString("imgs");
				Product product=new Product(productId,productName,productCategoryId,imgs);
				productList.add(product);
				
			}
			return productList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
			
		}

	}
	
	//首页精选案例刷新会随机换6个不一样的图片
	public List<Product> getProductList3(){
		List<Product> productList =new ArrayList<Product>();
		Connection conn = (Connection) DBManager.getConnection();
		Statement st=null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			
			rs = st.executeQuery("select * from product order by rand() limit 6");
			while (rs.next()) {
				int productId=rs.getInt("productId");
				String productName=rs.getString("productName");
				int productCategoryId=rs.getInt("productCategoryId");
				String imgs=rs.getString("imgs");
				Product product=new Product(productId,productName,productCategoryId,imgs);
				productList.add(product);
				
			}
			return productList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
			
		}

	}
}
	


	

	
